package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.DataAccess;

/**
 * Servlet implementation class Controller
 */
public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Controller() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if ("makeEntry".equals(request.getParameter("whereFrom"))) {
			boolean requestStatus = DataAccess
					.makeEntry(
							(String) request.getParameter("GEN_Client"),
							(String) request.getParameter("GEN_flangeID"),
							(String) request.getParameter("GEN_flangeLocation"),
							(String) request.getParameter("GEN_HBTSupervisor"),
							(String) request.getParameter("GEN_HBTTechnician"),
							(String) request.getParameter("GEN_Workdate"),
							(String) request.getParameter("JIR_top"),
							(String) request.getParameter("JIR_bottom"),
							(String) request.getParameter("JIR_left"),
							(String) request.getParameter("JIR_right"),
							(String) request.getParameter("JIR_remarks"),
							(String) request
									.getParameter("FD_nomDiaFlangeClass"),
							(String) request.getParameter("FD_gasketType"),
							(String) request.getParameter("FD_gasketMaterial"),
							(String) request.getParameter("FD_boltMaterial"),
							(String) request.getParameter("FD_flangeMaterial"),
							(String) request.getParameter("FD_boltDiameter"),
							(String) request.getParameter("FD_noOfBolts"),
							(String) request
									.getParameter("FD_jointConfiguration"),
							(String) request
									.getParameter("FD_flangeLineTestPressure"),
							(String) request
									.getParameter("BTenR_noOfTensioner"),
							(String) request.getParameter("BTenR_pumpSerialNo"),
							(String) request
									.getParameter("BTenR_1stPassPressure"),
							(String) request
									.getParameter("BTenR_2ndPassPressure"),
							(String) request
									.getParameter("BTenR_CheckingPassPressure"),
							(String) request.getParameter("VC_boltCondition"),
							(String) request
									.getParameter("VC_flangePreparedBy"),
							(String) request
									.getParameter("VC_minThreadProtrusion"),
							(String) request.getParameter("VC_threadForm"),
							(String) request
									.getParameter("VC_flangeFaceCondition"),
							(String) request.getParameter("ET_boltTensioner"),
							(String) request
									.getParameter("ET_boltTensionerModel"),
							(String) request.getParameter("ET_torqueWrench"),
							(String) request
									.getParameter("ET_torqueWrenchModel"),
							(String) request.getParameter("ET_floggingSpanner"),
							(String) request
									.getParameter("ET_floggingSpannerModel"),
							(String) request
									.getParameter("BTorR_noOfTorqueWrench"),
							(String) request.getParameter("BTorR_pumpSerialNo"),
							(String) request
									.getParameter("BTorR_lubricantUsed"),
							(String) request
									.getParameter("BTorR_lubricantAppliedBy"),
							(String) request
									.getParameter("BTorR_torqueApplied"),
							(String) request
									.getParameter("JRL_residualBoltLoad"),
							(String) request
									.getParameter("JRL_residualBoltStress"),
							(String) request.getParameter("JRL_torque"),
							(String) request
									.getParameter("JRL_frictionCoefficient"),
							(String) request
									.getParameter("JRL_pressureApplied"),
							(String) request
									.getParameter("CSL_residualBoltLoad"),
							(String) request
									.getParameter("CSL_residualBoltStress"),
							(String) request.getParameter("CSL_torque"),
							(String) request.getParameter("RAS_justriteName"),
							(String) request.getParameter("RAS_justriteDate"),
							(String) request.getParameter("RAS_onBehalfOf"),
							(String) request.getParameter("RAS_onBehalfOfName"),
							(String) request.getParameter("RAS_onBehalfOfDate"),
							(String) request
									.getParameter("RAS_flangeTagsAttached"));
			if (requestStatus) {
				request.setAttribute("message", "success");
				request.getRequestDispatcher("JSP/FlangeData-create.jsp").forward(request, response);
			} else {
				request.setAttribute("message",
						"snapped");
				request.getRequestDispatcher("JSP/FlangeData-create.jsp").forward(request, response);
			}
		}

		if (("searchByClient").equals((String) request
				.getParameter("whereFrom"))) {

			String ClientName = (String) request
					.getParameter("TXTsearchByClient");
			ArrayList<String> AList = dao.DataAccess.fetchList(ClientName);
			request.setAttribute("AList", AList);

			request.getRequestDispatcher("JSP/FlangeData-searchResults.jsp")
					.forward(request, response);

		}
		if (("searchByFlangeID").equals((String) request
				.getParameter("whereFrom"))) {

			String ClientName = (String) request
					.getParameter("searchByFlangeID");
			ArrayList<String> AList = dao.DataAccess.fetchListbyID(ClientName);
			request.setAttribute("AList", AList);

			request.getRequestDispatcher("JSP/FlangeData-searchResults.jsp")
					.forward(request, response);

		}
		if (("searchByFlangeLocation").equals((String) request
				.getParameter("whereFrom"))) {

			String ClientName = (String) request
					.getParameter("searchByFlangeLocation");
			ArrayList<String> AList = dao.DataAccess
					.fetchListbyLocation(ClientName);
			request.setAttribute("AList", AList);

			request.getRequestDispatcher("JSP/FlangeData-searchResults.jsp")
					.forward(request, response);

		}

	}

}
